package thread;

public class TestLock {
    public static void main(String[] args) {
        Account out=new Account( 100);
        Account in=new Account(120);
        new Thread(new Dead(out,in,50),"a").start();
        new Thread(new Dead(out,in,50),"b").start();
    }

}
class Account {
    double money;

    public Account(double money) {
        this.money = money;
    }
}
class Dead implements Runnable{
    private static Account out;
    private static Account in;
    private Double transfor;

    public Dead(Account out, Account in, double transfor) {
        this.out = out;
        this.in = in;
        this.transfor = transfor;
    }

    @Override
    public void run() {
        synchronized (out){
            out.money=out.money-transfor;
            System.out.println(Thread.currentThread().getName()+"转出"+transfor);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            in.money=in.money+transfor;
        }
        synchronized (in){
            in.money=in.money+transfor;

            System.out.println(Thread.currentThread().getName()+"被转入"+transfor);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            out.money=out.money-transfor;
        }
    }
}
